package com.leather.admin.controller;

import com.leather.admin.feignclient.JobClient;
import com.leather.common.entity.Job;
import com.leather.common.toolkits.DateKit;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

@Controller
@RequestMapping("admin/job")
public class JobController {

    @Autowired
    private JobClient jobClient;

    @RequiresPermissions("admin:job:index")
    @GetMapping("/index")
    public String index() {
        return "job/index";
    }


    @RequiresPermissions("admin:job:create")
    @GetMapping("/create")
    public String create() {
        return "job/create";
    }

    @RequiresPermissions("admin:job:index")
    @GetMapping("/list")
    @ResponseBody
    public Object list(
            @RequestParam(required = false, defaultValue = "0") int offset,
            @RequestParam(required = false, defaultValue = "10") int limit,
            @RequestParam(required = false, defaultValue = "") String search
    ) {
        return jobClient.list(offset, limit, search);
    }

    @ResponseBody
    @PostMapping("/create")
    @RequiresPermissions("admin:job:create")
    public Object addJob(Job job) {
        job.setCreateTime(DateKit.now());
        return jobClient.addJob(job);
    }


    @ResponseBody
    @PostMapping("/delete")
    @RequiresPermissions("admin:job:delete")
    public Object deletJob(@RequestParam("jobId") int jobId) {
        return jobClient.deleteJob(jobId);
    }

}
